<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>taro+canvas 画圆形进度条+canvas 转图片 | Beiysd</title>
    <meta name="generator" content="VuePress 1.8.2">
    <link rel="manifest" href="/manifest.json">
    <link rel="apple-touch-icon" href="/assets/logo/pkp.png">
    <script>var _hmt = _hmt || [];
      (function() {
        var hm = document.createElement("script");
        hm.src = "https://hm.baidu.com/hm.js?345ab2bf830dc5c68a96925eb1dd0b45";
        var s = document.getElementsByTagName("script")[0]; 
        s.parentNode.insertBefore(hm, s);
      })();</script>
    <meta name="description" content="Live and Study.">
    
    <link rel="preload" href="/assets/css/0.styles.bd7bd1ca.css" as="style"><link rel="preload" href="/assets/js/app.17b6b866.js" as="script"><link rel="preload" href="/assets/js/3.16fca902.js" as="script"><link rel="preload" href="/assets/js/2.aa0e2402.js" as="script"><link rel="preload" href="/assets/js/54.f2b42c40.js" as="script"><link rel="prefetch" href="/assets/js/10.71654f5d.js"><link rel="prefetch" href="/assets/js/11.e3df8a38.js"><link rel="prefetch" href="/assets/js/12.f6e1589f.js"><link rel="prefetch" href="/assets/js/13.8ac48caf.js"><link rel="prefetch" href="/assets/js/14.2a93538f.js"><link rel="prefetch" href="/assets/js/15.ed5b285c.js"><link rel="prefetch" href="/assets/js/16.5e1e59d5.js"><link rel="prefetch" href="/assets/js/17.1da7896e.js"><link rel="prefetch" href="/assets/js/18.6f1b273c.js"><link rel="prefetch" href="/assets/js/19.30d92fa3.js"><link rel="prefetch" href="/assets/js/20.1068c4cc.js"><link rel="prefetch" href="/assets/js/21.f3d90412.js"><link rel="prefetch" href="/assets/js/22.a3c35868.js"><link rel="prefetch" href="/assets/js/23.3dfaf7a4.js"><link rel="prefetch" href="/assets/js/24.b7562cb8.js"><link rel="prefetch" href="/assets/js/25.94bbff07.js"><link rel="prefetch" href="/assets/js/26.9cbdf92e.js"><link rel="prefetch" href="/assets/js/27.e2189a25.js"><link rel="prefetch" href="/assets/js/28.12e893b0.js"><link rel="prefetch" href="/assets/js/29.ab1115c9.js"><link rel="prefetch" href="/assets/js/30.e2112157.js"><link rel="prefetch" href="/assets/js/31.76cdc758.js"><link rel="prefetch" href="/assets/js/32.c94896d8.js"><link rel="prefetch" href="/assets/js/33.0841fa36.js"><link rel="prefetch" href="/assets/js/34.831fd380.js"><link rel="prefetch" href="/assets/js/35.b0449875.js"><link rel="prefetch" href="/assets/js/36.30a8c074.js"><link rel="prefetch" href="/assets/js/37.1aec4dbc.js"><link rel="prefetch" href="/assets/js/38.ccd6cdf9.js"><link rel="prefetch" href="/assets/js/39.c10337d7.js"><link rel="prefetch" href="/assets/js/4.98a034f9.js"><link rel="prefetch" href="/assets/js/40.addec5bb.js"><link rel="prefetch" href="/assets/js/41.e3e62de0.js"><link rel="prefetch" href="/assets/js/42.ba7501f2.js"><link rel="prefetch" href="/assets/js/43.d8fa5ea0.js"><link rel="prefetch" href="/assets/js/44.d6dbcdb3.js"><link rel="prefetch" href="/assets/js/45.fe55507d.js"><link rel="prefetch" href="/assets/js/46.6af0f2df.js"><link rel="prefetch" href="/assets/js/47.0560a688.js"><link rel="prefetch" href="/assets/js/48.ab74b154.js"><link rel="prefetch" href="/assets/js/49.7ed2bcd8.js"><link rel="prefetch" href="/assets/js/5.2ca1c567.js"><link rel="prefetch" href="/assets/js/50.0f22d3af.js"><link rel="prefetch" href="/assets/js/51.ad770490.js"><link rel="prefetch" href="/assets/js/52.bc1bd98c.js"><link rel="prefetch" href="/assets/js/53.9ff88855.js"><link rel="prefetch" href="/assets/js/55.3d551cd8.js"><link rel="prefetch" href="/assets/js/56.bbf7abd0.js"><link rel="prefetch" href="/assets/js/57.4b2fbc05.js"><link rel="prefetch" href="/assets/js/58.4ff6654e.js"><link rel="prefetch" href="/assets/js/59.92969875.js"><link rel="prefetch" href="/assets/js/6.f1011be9.js"><link rel="prefetch" href="/assets/js/60.4fb5be24.js"><link rel="prefetch" href="/assets/js/61.661f240d.js"><link rel="prefetch" href="/assets/js/62.d08a0fee.js"><link rel="prefetch" href="/assets/js/63.607fa9fb.js"><link rel="prefetch" href="/assets/js/64.bfa429e4.js"><link rel="prefetch" href="/assets/js/65.e184bc74.js"><link rel="prefetch" href="/assets/js/7.28ea5937.js"><link rel="prefetch" href="/assets/js/8.d804616d.js"><link rel="prefetch" href="/assets/js/9.7977e413.js">
    <link rel="stylesheet" href="/assets/css/0.styles.bd7bd1ca.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/assets/logo/pkq.png" alt="Beiysd" class="logo"> <span class="site-name can-hide">Beiysd</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="https://github.com/Beiysd" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Github
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div><div class="nav-item"><a href="https://gitee.com/beiysd" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Gitee
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div><div class="nav-item"><a href="https://blog.csdn.net/weixin_40532650" target="_blank" rel="noopener noreferrer" class="nav-link external">
  CSDN
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="https://github.com/Beiysd" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Github
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div><div class="nav-item"><a href="https://gitee.com/beiysd" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Gitee
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div><div class="nav-item"><a href="https://blog.csdn.net/weixin_40532650" target="_blank" rel="noopener noreferrer" class="nav-link external">
  CSDN
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>前端</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><section class="sidebar-group collapsable is-sub-group depth-1"><p class="sidebar-heading"><span>CSS</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable is-sub-group depth-1"><p class="sidebar-heading"><span>常用方法</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable is-sub-group depth-1"><p class="sidebar-heading"><span>React</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable is-sub-group depth-1"><p class="sidebar-heading"><span>JavaScript</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable is-sub-group depth-1"><p class="sidebar-heading"><span>Vuepress</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable is-sub-group depth-1"><p class="sidebar-heading open"><span>Taro小程序</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/notes/10-前端/60-Taro小程序/less_sass插件安装报错问题.html" class="sidebar-link">less_sass插件安装报错问题</a></li><li><a href="/notes/10-前端/60-Taro小程序/React模板生命周期.html" class="sidebar-link">React模板生命周期</a></li><li><a href="/notes/10-前端/60-Taro小程序/taro+canvas画圆形进度条+canvas转图片.html" class="active sidebar-link">taro+canvas画圆形进度条+canvas转图片</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/notes/10-前端/60-Taro小程序/taro_h5并行开发.html" class="sidebar-link">taro_h5并行开发</a></li></ul></section></li></ul></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>后端</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>随笔</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>杂谈</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>开发相关</span> <span class="arrow right"></span></p> <!----></section></li></ul> </aside> <main class="page"><h3 class="page_title">taro+canvas 画圆形进度条+canvas 转图片</h3> <ul class="uls"><li title="标签" class="lis"><img src="/assets/img/tag.f21efeeb.svg" class="icons">
    </li> <li title="更新时间" class="lis"><img src="/assets/img/clock.ffb2d0be.svg" class="icons">2021-04-16
    </li> <li title="浏览人数" class="lis"><img src="/assets/img/eye.e9087eed.svg" class="icons">0
    </li></ul>  <div class="theme-default-content content__default"><h3 id="taro-canvas-画圆形进度条-canvas-转图片"><a href="#taro-canvas-画圆形进度条-canvas-转图片" class="header-anchor">#</a> taro+canvas 画圆形进度条+canvas 转图片</h3> <ul><li>效果</li></ul> <p><img src="/assets/md-imgs/canvas-01.png" alt="效果"></p> <ul><li>代码</li></ul> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">/**
 * @name ProgresCanvas
 * @desc 进度条
 * @taro 3.07
 * @date 2020/08/06
 */</span>

<span class="token keyword">import</span> React<span class="token punctuation">,</span> <span class="token punctuation">{</span> Component <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">&quot;react&quot;</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> Taro <span class="token keyword">from</span> <span class="token string">&quot;@tarojs/taro&quot;</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token punctuation">{</span> View<span class="token punctuation">,</span> Canvas<span class="token punctuation">,</span> Image<span class="token punctuation">,</span> CoverView <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">&quot;@tarojs/components&quot;</span><span class="token punctuation">;</span>

<span class="token keyword">import</span> styles <span class="token keyword">from</span> <span class="token string">&quot;./index.module.scss&quot;</span><span class="token punctuation">;</span>

<span class="token keyword">let</span> clientHeight <span class="token operator">=</span> Taro<span class="token punctuation">.</span>$navBarWindowHeight<span class="token punctuation">;</span> <span class="token comment">//可使用窗口高</span>
<span class="token keyword">let</span> clientWidth <span class="token operator">=</span> Taro<span class="token punctuation">.</span>$navBarWindowWidth<span class="token punctuation">;</span> <span class="token comment">//可使用窗口宽</span>

<span class="token keyword">let</span> ratio<span class="token punctuation">;</span> <span class="token comment">//根据尺寸动态计算 1px换算成多少rpx</span>

<span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token keyword">class</span> <span class="token class-name">ProgresCanvas</span> <span class="token keyword">extends</span> <span class="token class-name">Component</span> <span class="token punctuation">{</span>
  <span class="token keyword">static</span> defaultProps <span class="token operator">=</span> <span class="token punctuation">{</span>
    data<span class="token operator">:</span> <span class="token string">&quot;lalalal&quot;</span><span class="token punctuation">,</span>
    color<span class="token operator">:</span> <span class="token string">&quot;#7d79f3&quot;</span><span class="token punctuation">,</span>
    progress<span class="token operator">:</span> <span class="token number">0</span>
  <span class="token punctuation">}</span><span class="token punctuation">;</span>

  state <span class="token operator">=</span> <span class="token punctuation">{</span> imageUrl<span class="token operator">:</span> <span class="token string">&quot;&quot;</span><span class="token punctuation">,</span> nowdata<span class="token operator">:</span> <span class="token string">&quot;&quot;</span> <span class="token punctuation">}</span><span class="token punctuation">;</span>

  <span class="token function">componentDidUpdate</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">drawProgressbg</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>

  <span class="token comment">//绘制背景</span>
  <span class="token function-variable function">drawProgressbg</span> <span class="token operator">=</span> <span class="token keyword">async</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    <span class="token comment">//stepone, steptwo, size, width, colorone, colortwo</span>
    <span class="token keyword">const</span> <span class="token punctuation">{</span> data <span class="token punctuation">}</span> <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>props<span class="token punctuation">;</span>
    <span class="token keyword">const</span> <span class="token punctuation">{</span> nowdata <span class="token punctuation">}</span> <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>state<span class="token punctuation">;</span>

    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">stringify</span><span class="token punctuation">(</span>data<span class="token punctuation">)</span> <span class="token operator">===</span> <span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">stringify</span><span class="token punctuation">(</span>nowdata<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token keyword">return</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>data<span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token comment">// console.log(&quot;progressData====&quot;, data, nowdata);</span>
      <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">setState</span><span class="token punctuation">(</span><span class="token punctuation">{</span> nowdata<span class="token operator">:</span> data <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      Taro<span class="token punctuation">.</span><span class="token function">getSystemInfo</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
        <span class="token function-variable function">success</span><span class="token operator">:</span> <span class="token punctuation">(</span><span class="token parameter">res</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
          clientWidth <span class="token operator">=</span> res<span class="token punctuation">.</span>windowWidth<span class="token punctuation">;</span>
          clientHeight <span class="token operator">=</span> res<span class="token punctuation">.</span>windowHeight<span class="token punctuation">;</span>
          <span class="token comment">//屏幕宽度 375px = 750rpx, 1px = rpx</span>
          <span class="token comment">// 1px = （750 / 屏幕宽度）rpx；</span>
          <span class="token comment">// 1rpx = （屏幕宽度 / 750）px;</span>
          ratio <span class="token operator">=</span> <span class="token number">550</span> <span class="token operator">/</span> clientWidth<span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
      <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

      <span class="token comment">// 使用 .createContext 获取绘图上下文 context</span>
      <span class="token keyword">const</span> <span class="token punctuation">{</span> canvas<span class="token punctuation">,</span> ctx <span class="token punctuation">}</span> <span class="token operator">=</span> <span class="token keyword">await</span> <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">getCanvasCtx</span><span class="token punctuation">(</span>data<span class="token punctuation">.</span>idone<span class="token punctuation">)</span><span class="token punctuation">;</span>
      ctx<span class="token punctuation">.</span>lineWidth <span class="token operator">=</span> <span class="token function">Number</span><span class="token punctuation">(</span>data<span class="token punctuation">.</span>width<span class="token punctuation">)</span><span class="token punctuation">;</span>
      ctx<span class="token punctuation">.</span>strokeStyle <span class="token operator">=</span> <span class="token template-string"><span class="token template-punctuation string">`</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>data<span class="token punctuation">.</span>colorone<span class="token interpolation-punctuation punctuation">}</span></span><span class="token template-punctuation string">`</span></span><span class="token punctuation">;</span>
      <span class="token comment">// ctx.lineCap = &quot;butt&quot;;</span>
      ctx<span class="token punctuation">.</span><span class="token function">beginPath</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//开始一个新的路径</span>
      ctx<span class="token punctuation">.</span><span class="token function">arc</span><span class="token punctuation">(</span><span class="token number">69</span> <span class="token operator">/</span> ratio<span class="token punctuation">,</span> <span class="token number">69</span> <span class="token operator">/</span> ratio<span class="token punctuation">,</span> <span class="token number">60</span> <span class="token operator">/</span> ratio<span class="token punctuation">,</span> <span class="token operator">-</span>Math<span class="token punctuation">.</span><span class="token constant">PI</span> <span class="token operator">/</span> data<span class="token punctuation">.</span>start<span class="token punctuation">,</span> data<span class="token punctuation">.</span>end <span class="token operator">===</span> <span class="token number">1</span> <span class="token operator">?</span> <span class="token operator">-</span><span class="token number">2</span> <span class="token operator">*</span> Math<span class="token punctuation">.</span><span class="token constant">PI</span> <span class="token operator">:</span> Math<span class="token punctuation">.</span><span class="token constant">PI</span> <span class="token operator">+</span> Math<span class="token punctuation">.</span><span class="token constant">PI</span> <span class="token operator">/</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//x,y,半径,开始,结束</span>
      <span class="token comment">// ctx.closePath();</span>
      ctx<span class="token punctuation">.</span><span class="token function">stroke</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//对当前路径进行描边</span>

      ctx<span class="token punctuation">.</span>strokeStyle <span class="token operator">=</span> <span class="token template-string"><span class="token template-punctuation string">`</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>data<span class="token punctuation">.</span>colortwo<span class="token interpolation-punctuation punctuation">}</span></span><span class="token template-punctuation string">`</span></span><span class="token punctuation">;</span>
      ctx<span class="token punctuation">.</span><span class="token function">beginPath</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token keyword">if</span> <span class="token punctuation">(</span>data<span class="token punctuation">.</span>end <span class="token operator">===</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        ctx<span class="token punctuation">.</span><span class="token function">arc</span><span class="token punctuation">(</span><span class="token number">69</span> <span class="token operator">/</span> ratio<span class="token punctuation">,</span> <span class="token number">69</span> <span class="token operator">/</span> ratio<span class="token punctuation">,</span> <span class="token number">60</span> <span class="token operator">/</span> ratio<span class="token punctuation">,</span> <span class="token operator">-</span>Math<span class="token punctuation">.</span><span class="token constant">PI</span> <span class="token operator">/</span> data<span class="token punctuation">.</span>start<span class="token punctuation">,</span> <span class="token function">Number</span><span class="token punctuation">(</span>data<span class="token punctuation">.</span>steptwo<span class="token punctuation">)</span> <span class="token operator">*</span> Math<span class="token punctuation">.</span><span class="token constant">PI</span> <span class="token operator">-</span> Math<span class="token punctuation">.</span><span class="token constant">PI</span><span class="token punctuation">,</span> <span class="token boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//x,y,半径,开始,结束</span>
      <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
        ctx<span class="token punctuation">.</span><span class="token function">arc</span><span class="token punctuation">(</span><span class="token number">69</span> <span class="token operator">/</span> ratio<span class="token punctuation">,</span> <span class="token number">69</span> <span class="token operator">/</span> ratio<span class="token punctuation">,</span> <span class="token number">60</span> <span class="token operator">/</span> ratio<span class="token punctuation">,</span> <span class="token operator">-</span>Math<span class="token punctuation">.</span><span class="token constant">PI</span> <span class="token operator">/</span> data<span class="token punctuation">.</span>start<span class="token punctuation">,</span> <span class="token function">Number</span><span class="token punctuation">(</span>data<span class="token punctuation">.</span>steptwo<span class="token punctuation">)</span> <span class="token operator">*</span> Math<span class="token punctuation">.</span><span class="token constant">PI</span> <span class="token operator">-</span> Math<span class="token punctuation">.</span><span class="token constant">PI</span> <span class="token operator">/</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//x,y,半径,开始,结束</span>
      <span class="token punctuation">}</span>

      ctx<span class="token punctuation">.</span><span class="token function">stroke</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

      <span class="token function">setTimeout</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
        <span class="token keyword">const</span> base64 <span class="token operator">=</span> canvas<span class="token punctuation">.</span><span class="token function">toDataURL</span><span class="token punctuation">(</span><span class="token string">&quot;jpg&quot;</span><span class="token punctuation">,</span> <span class="token number">0.6</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">setState</span><span class="token punctuation">(</span><span class="token punctuation">{</span> imageUrl<span class="token operator">:</span> base64 <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token number">500</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span><span class="token punctuation">;</span>

  getCanvasCtx <span class="token operator">=</span> <span class="token keyword">async</span> <span class="token punctuation">(</span>eleId <span class="token operator">=</span> <span class="token string">&quot;&quot;</span><span class="token punctuation">,</span> delay <span class="token operator">=</span> <span class="token number">200</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">Promise</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">resolve<span class="token punctuation">,</span> reject</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
      <span class="token keyword">const</span> t <span class="token operator">=</span> <span class="token function">setTimeout</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
        <span class="token function">clearTimeout</span><span class="token punctuation">(</span>t<span class="token punctuation">)</span><span class="token punctuation">;</span>
        Taro<span class="token punctuation">.</span><span class="token function">createSelectorQuery</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
          <span class="token punctuation">.</span><span class="token function">select</span><span class="token punctuation">(</span><span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">#</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>eleId<span class="token interpolation-punctuation punctuation">}</span></span><span class="token template-punctuation string">`</span></span><span class="token punctuation">)</span>
          <span class="token punctuation">.</span><span class="token function">fields</span><span class="token punctuation">(</span><span class="token punctuation">{</span> node<span class="token operator">:</span> <span class="token boolean">true</span> <span class="token punctuation">}</span><span class="token punctuation">)</span>
          <span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">res</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>res <span class="token operator">&amp;&amp;</span> res<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">&amp;&amp;</span> res<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>node<span class="token punctuation">)</span> <span class="token punctuation">{</span>
              <span class="token keyword">const</span> canvas <span class="token operator">=</span> res<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>node<span class="token punctuation">;</span>
              <span class="token keyword">const</span> ctx <span class="token operator">=</span> canvas<span class="token punctuation">.</span><span class="token function">getContext</span><span class="token punctuation">(</span><span class="token string">&quot;2d&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
              <span class="token function">resolve</span><span class="token punctuation">(</span><span class="token punctuation">{</span> canvas<span class="token punctuation">,</span> ctx <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
              <span class="token function">reject</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
          <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token punctuation">}</span><span class="token punctuation">,</span> delay<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span><span class="token punctuation">;</span>

  <span class="token function">render</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">const</span> <span class="token punctuation">{</span> data <span class="token punctuation">}</span> <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>props<span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token punctuation">(</span>
      <span class="token operator">&lt;</span>CoverView className<span class="token operator">=</span><span class="token punctuation">{</span>styles<span class="token punctuation">.</span>progress_box<span class="token punctuation">}</span> style<span class="token operator">=</span><span class="token punctuation">{</span><span class="token punctuation">{</span> zIndex<span class="token operator">:</span> <span class="token number">1</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token operator">&gt;</span>
        <span class="token operator">&lt;</span>Image src<span class="token operator">=</span><span class="token punctuation">{</span><span class="token keyword">this</span><span class="token punctuation">.</span>state<span class="token punctuation">.</span>imageUrl<span class="token punctuation">}</span> className<span class="token operator">=</span><span class="token punctuation">{</span>styles<span class="token punctuation">.</span>img<span class="token punctuation">}</span> <span class="token operator">/</span><span class="token operator">&gt;</span>

        <span class="token operator">&lt;</span>Canvas className<span class="token operator">=</span><span class="token punctuation">{</span>styles<span class="token punctuation">.</span>progress_bg<span class="token punctuation">}</span> type<span class="token operator">=</span><span class="token string">&quot;2d&quot;</span> id<span class="token operator">=</span><span class="token punctuation">{</span>data<span class="token punctuation">.</span>idone<span class="token punctuation">}</span> canvas<span class="token operator">-</span>id<span class="token operator">=</span><span class="token punctuation">{</span>data<span class="token punctuation">.</span>idone<span class="token punctuation">}</span><span class="token operator">&gt;</span><span class="token operator">&lt;</span><span class="token operator">/</span>Canvas<span class="token operator">&gt;</span>
      <span class="token operator">&lt;</span><span class="token operator">/</span>CoverView<span class="token operator">&gt;</span>
    <span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token comment">/**style样式*/</span>
<span class="token punctuation">.</span>progress_box <span class="token punctuation">{</span>
  width<span class="token operator">:</span> <span class="token number">100</span><span class="token operator">%</span><span class="token punctuation">;</span>
  height<span class="token operator">:</span> <span class="token number">100</span><span class="token operator">%</span><span class="token punctuation">;</span>
  position<span class="token operator">:</span> relative<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token punctuation">.</span>progress_bg <span class="token punctuation">{</span>
  position<span class="token operator">:</span> absolute<span class="token punctuation">;</span>
  transform<span class="token operator">:</span> <span class="token function">translateX</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">200</span>vw<span class="token punctuation">)</span><span class="token punctuation">;</span>
  top<span class="token operator">:</span> <span class="token number">10</span><span class="token operator">%</span><span class="token punctuation">;</span>
  left<span class="token operator">:</span> <span class="token number">10</span><span class="token operator">%</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token punctuation">.</span>img <span class="token punctuation">{</span>
  margin<span class="token operator">-</span>top<span class="token operator">:</span> <span class="token number">4</span><span class="token operator">%</span><span class="token punctuation">;</span>
  margin<span class="token operator">-</span>left<span class="token operator">:</span> <span class="token number">4</span><span class="token operator">%</span><span class="token punctuation">;</span>
  width<span class="token operator">:</span> <span class="token number">640</span>px<span class="token punctuation">;</span>
  height<span class="token operator">:</span> <span class="token number">320</span>px<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment">/**使用方法*/</span>
<span class="token keyword">import</span> ProgresCanvas <span class="token keyword">from</span> <span class="token string">&quot;@/components/Progress&quot;</span><span class="token punctuation">;</span><span class="token comment">//页面引入</span>
<span class="token keyword">const</span> progData <span class="token operator">=</span> <span class="token punctuation">{</span>
      stepone<span class="token operator">:</span> <span class="token number">2</span><span class="token punctuation">,</span><span class="token comment">//底层圆进度，默认满圆</span>
      steptwo<span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span><span class="token comment">//上层圆进度</span>
      size<span class="token operator">:</span> <span class="token number">1.2</span><span class="token punctuation">,</span><span class="token comment">//大小</span>
      width<span class="token operator">:</span> <span class="token number">8</span><span class="token punctuation">,</span><span class="token comment">//宽度</span>
      colorone<span class="token operator">:</span> <span class="token string">&quot;#E7E7E7&quot;</span><span class="token punctuation">,</span><span class="token comment">//底层圆颜色</span>
      colortwo<span class="token operator">:</span> <span class="token string">&quot;#FFB17D&quot;</span><span class="token punctuation">,</span><span class="token comment">//上层圆颜色</span>
      idone<span class="token operator">:</span> <span class="token string">&quot;bigdata_pro0101&quot;</span><span class="token punctuation">,</span><span class="token comment">//底层圆id(可用随机数)</span>
      idtwo<span class="token operator">:</span> <span class="token string">&quot;bigdata_pro0102&quot;</span><span class="token punctuation">,</span><span class="token comment">//上层圆id</span>
      start<span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span><span class="token comment">//开始位置（不需要可屏蔽，相对应progress里面做相应屏蔽）</span>
      end<span class="token operator">:</span> <span class="token number">1</span><span class="token comment">//控制整圆or半圆</span>
    <span class="token punctuation">}</span><span class="token punctuation">;</span>
    
    <span class="token operator">&lt;</span>ProgresCanvas data<span class="token operator">=</span><span class="token punctuation">{</span>progData<span class="token punctuation">}</span> width<span class="token operator">=</span><span class="token punctuation">{</span>width<span class="token punctuation">}</span> <span class="token operator">/</span><span class="token operator">&gt;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br><span class="line-number">52</span><br><span class="line-number">53</span><br><span class="line-number">54</span><br><span class="line-number">55</span><br><span class="line-number">56</span><br><span class="line-number">57</span><br><span class="line-number">58</span><br><span class="line-number">59</span><br><span class="line-number">60</span><br><span class="line-number">61</span><br><span class="line-number">62</span><br><span class="line-number">63</span><br><span class="line-number">64</span><br><span class="line-number">65</span><br><span class="line-number">66</span><br><span class="line-number">67</span><br><span class="line-number">68</span><br><span class="line-number">69</span><br><span class="line-number">70</span><br><span class="line-number">71</span><br><span class="line-number">72</span><br><span class="line-number">73</span><br><span class="line-number">74</span><br><span class="line-number">75</span><br><span class="line-number">76</span><br><span class="line-number">77</span><br><span class="line-number">78</span><br><span class="line-number">79</span><br><span class="line-number">80</span><br><span class="line-number">81</span><br><span class="line-number">82</span><br><span class="line-number">83</span><br><span class="line-number">84</span><br><span class="line-number">85</span><br><span class="line-number">86</span><br><span class="line-number">87</span><br><span class="line-number">88</span><br><span class="line-number">89</span><br><span class="line-number">90</span><br><span class="line-number">91</span><br><span class="line-number">92</span><br><span class="line-number">93</span><br><span class="line-number">94</span><br><span class="line-number">95</span><br><span class="line-number">96</span><br><span class="line-number">97</span><br><span class="line-number">98</span><br><span class="line-number">99</span><br><span class="line-number">100</span><br><span class="line-number">101</span><br><span class="line-number">102</span><br><span class="line-number">103</span><br><span class="line-number">104</span><br><span class="line-number">105</span><br><span class="line-number">106</span><br><span class="line-number">107</span><br><span class="line-number">108</span><br><span class="line-number">109</span><br><span class="line-number">110</span><br><span class="line-number">111</span><br><span class="line-number">112</span><br><span class="line-number">113</span><br><span class="line-number">114</span><br><span class="line-number">115</span><br><span class="line-number">116</span><br><span class="line-number">117</span><br><span class="line-number">118</span><br><span class="line-number">119</span><br><span class="line-number">120</span><br><span class="line-number">121</span><br><span class="line-number">122</span><br><span class="line-number">123</span><br><span class="line-number">124</span><br><span class="line-number">125</span><br><span class="line-number">126</span><br><span class="line-number">127</span><br><span class="line-number">128</span><br><span class="line-number">129</span><br><span class="line-number">130</span><br><span class="line-number">131</span><br><span class="line-number">132</span><br><span class="line-number">133</span><br><span class="line-number">134</span><br><span class="line-number">135</span><br><span class="line-number">136</span><br><span class="line-number">137</span><br><span class="line-number">138</span><br><span class="line-number">139</span><br><span class="line-number">140</span><br><span class="line-number">141</span><br><span class="line-number">142</span><br><span class="line-number">143</span><br><span class="line-number">144</span><br><span class="line-number">145</span><br><span class="line-number">146</span><br><span class="line-number">147</span><br><span class="line-number">148</span><br><span class="line-number">149</span><br></div></div></div> <div class="page-nav"><p class="inner"><span class="prev">
      ←
      <a href="/notes/10-前端/60-Taro小程序/React模板生命周期.html" class="prev">
        React模板生命周期
      </a></span> <span class="next"><a href="/notes/10-前端/60-Taro小程序/taro_h5并行开发.html">
        taro_h5并行开发
      </a>
      →
    </span></p></div>  <button>click</button> <div class="back_top_content" data-v-26f75736><!----></div></main></div><div class="global-ui"><MySWUpdatePopup></MySWUpdatePopup></div></div>
    <script src="/assets/js/app.17b6b866.js" defer></script><script src="/assets/js/3.16fca902.js" defer></script><script src="/assets/js/2.aa0e2402.js" defer></script><script src="/assets/js/54.f2b42c40.js" defer></script>
  </body>
</html>
